#include<iostream>
#include<vector>
#include<queue>
using namespace std;


struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};
class Solution {
public:
    vector<double> averageOfLevels(TreeNode* root) {
        int level = 0;
        int levelSize = 1;
        int tmpCount = 0;

        vector<double> ans;
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {
            double tmpSum = 0;
            int size = q.size();
            for (int i = 0; i < size; i++) {
                auto node = q.front();
                q.pop();
                tmpSum += node->val;
                auto left = node->left, right = node->right;
                if (left != nullptr) q.push(left);
                if (right != nullptr) q.push(right);
            }
            ans.push_back(tmpSum / size);
        }
        return ans;
    }
};